{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取官网AK和SK，请求token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "#!/usr/bin/env python\n",
    " \n",
    "import urllib\n",
    "import base64\n",
    "import json\n",
    "#client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "client_id = 'tcGgx5LKZsjiUqp5xVV5c7G1'\n",
    "client_secret = '6H7xQDPcTQ2HQwgyCOdUB8uVB0bWvnae'\n",
    " \n",
    "#获取token\n",
    "def get_token():\n",
    "    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret\n",
    "    request = urllib.request.Request(host)\n",
    "    request.add_header('Content-Type', 'application/json; charset=UTF-8')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    token_content = response.read()\n",
    "    if token_content:\n",
    "        token_info = json.loads(token_content)\n",
    "        token_key = token_info['access_token']\n",
    "    return token_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用“红酒识别”API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 识别红酒图片：![选自百度AI平台](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9haS5iZHN0YXRpYy5jb20vZmlsZS8zOTJDMjI5NjAyQjY0QjAxQkY0QkJGOEMzNjEwMjM3Qg?x-oss-process=image/format,png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "红酒1\n",
      "处理时长:5.39秒\n",
      "红酒名称: 旋转木马阿斯蒂莫斯卡托甜起泡酒\n",
      "国家: 意大利\n",
      "产区: 皮埃蒙特\n",
      "子产区: 阿斯蒂莫斯卡托优质法定产区\n",
      "酒庄: 佳妮酒庄\n",
      "酒类型: 起泡酒/Sparkling Wine\n",
      "糖分类型: 甜型/Sweet\n",
      "色泽: 深柠檬色/Dark Lemon\n",
      "葡萄品种: \n",
      "品尝温度: 6-11°C\n",
      "特点说明: 该酒呈现深柠檬色，散发出柠檬、蜂蜜的香气，酒体轻盈，酸度适中。\n"
     ]
    }
   ],
   "source": [
    "#红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/hongjiu2.png', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('红酒1')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 选用不同角度的图片，确定API对于红酒识别图片要求"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 红酒1：图标清楚，酒身全显示，除红酒外有其他物体\n",
    "\n",
    "![红酒1](https://tse3-mm.cn.bing.net/th/id/OIP.HQnaLRTXx53GW38u0mxbcQHaJ1?pid=Api&rs=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标红酒1\n",
      "处理时长:1.34秒\n",
      "红酒名称: \n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/hognjiu_1.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('目标红酒1')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 由输出结果来看，可以得知，物体不清晰的红酒，无法被识别。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 红酒2：重点拍摄图标，酒身全显示，但有点模糊的酒标。\n",
    "![红酒3](http://img.jdzj.com/UserDocument/2015c/javen398/Picture/20167614554.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标红酒2\n",
      "处理时长:0.88秒\n",
      "红酒名称: Montagne Saint-Émilion\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/hongjiu_2.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('目标红酒2')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 由输出结果来看，重点拍摄图标可以识别出红酒的名称，但未能识别出详细的信息。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 红酒3：图标清楚，酒身全显示，只有红酒。\n",
    "![红酒2](https://tse3-mm.cn.bing.net/th/id/OIP.wRsTmdxR13Qw5YEPu_nsBAHaFH?pid=Api&rs=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标红酒3\n",
      "处理时长:0.58秒\n",
      "红酒名称: \n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/hongjiu_3.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('目标红酒3')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 从输出结果来看，只有红酒一个主体，但是未能识别出酒标中的信息。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 红酒4：只拍摄酒标，并图片清晰。\n",
    "\n",
    "![红酒4](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9haS5iZHN0YXRpYy5jb20vZmlsZS9DNDQ4M0U1OEM1NjA0NTJFOEZCRjhGOUJDRkFGMTMzQw?x-oss-process=image/format,png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标红酒4\n",
      "处理时长:0.87秒\n",
      "红酒名称: \n",
      "国家: 澳大利亚\n",
      "产区: 南澳\n",
      "子产区: 克来尔谷\n",
      "酒庄: \n",
      "酒类型: 红葡萄酒/Red Wine\n",
      "糖分类型: 干型/Dry\n",
      "色泽: 紫红色/Violet\n",
      "葡萄品种: \n",
      "品尝温度: 14-18°C\n",
      "特点说明: 此酒呈紫红色，带有覆盆子、黑莓、皮革和辛烈香的香气，口感柔和，单宁丰厚，果味浓郁。\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/hongjiu_4.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('目标红酒4')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 从输出结果来看，只拍摄酒标，并且酒标清晰的图，可以被精准识别，并且能够识别出详细信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 选用不同的数据图片对红酒识别API精准度进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "处理时长:0.42秒\n",
      "红酒名称: 费拉里斯酒庄代表作红葡萄酒\n",
      "国家: 意大利\n",
      "产区: 皮埃蒙特\n",
      "子产区: 卢克梦费拉多优质法定产区\n",
      "酒庄: \n",
      "酒类型: 红葡萄酒/Red Wine\n",
      "糖分类型: 干型/Dry\n",
      "色泽: 深宝石红色/Dark Ruby\n",
      "葡萄品种: \n",
      "品尝温度: 14-18℃\n",
      "特点说明: 此酒呈深宝石红色，香气复杂，带有成熟的红色水果、香料、香草以及甘草的香气，中等单宁，酸度适中，结构平衡，有成熟的覆盆子以及樱桃的味道。\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/1.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('1')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "处理时长:0.44秒\n",
      "红酒名称: 绅洛酒庄坎博斯红葡萄酒\n",
      "国家: 意大利\n",
      "产区: 皮埃蒙特\n",
      "子产区: 巴罗洛优质法定产区\n",
      "酒庄: 绅洛酒庄\n",
      "酒类型: 红葡萄酒/Red Wine\n",
      "糖分类型: 干型/Dry\n",
      "色泽: 浅石榴红色/Light Garnet\n",
      "葡萄品种: \n",
      "品尝温度: 14-18℃\n",
      "特点说明: 此酒带有木炭、烟雾和梅花的香气，口感浓郁，具有天鹅绒般的单宁和纯李子、树莓的味道。\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/2.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('2')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "处理时长:0.43秒\n",
      "红酒名称: 蒙特斯紫天使红葡萄酒\n",
      "国家: 智利\n",
      "产区: 中央谷地\n",
      "子产区: 空加瓜谷\n",
      "酒庄: 蒙特斯酒庄\n",
      "酒类型: 红葡萄酒/Red Wine\n",
      "糖分类型: 干型/Dry\n",
      "色泽: 深紫红色/Dark Violet\n",
      "葡萄品种: \n",
      "品尝温度: 14-18℃\n",
      "特点说明: 此酒呈深紫红色，散发黑果风味及带点凉爽地区马齐古的凯奈尔的辛辣轻快果香，亦有小味儿多的强烈黑浆果味的香气，酒体丰厚，大量成熟的单宁赋予收敛感并令酒体结构更佳。\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/3.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('3')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "处理时长:0.39秒\n",
      "红酒名称: 法国 苏维斯干红葡萄酒\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/4.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('4')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "处理时长:0.37秒\n",
      "红酒名称: 法国葡萄酒 拉菲罗氏传奇梅多克红葡萄酒\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/5.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('5')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图片中有多瓶酒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "处理时长:0.26秒\n",
      "红酒名称: 法国安祖路登佰涅城堡干白葡萄酒\n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/6.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('6')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "处理时长:0.40秒\n",
      "红酒名称: \n"
     ]
    }
   ],
   "source": [
    "# 红酒识别\n",
    "#filename:图片名（本地存储包括路径）\n",
    "def redwine(filename):\n",
    "    request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine\"\n",
    "    print(filename)\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open('E:/Administrator/2020大二（下）/API/期末项目/7.jpg', 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = dict()\n",
    "    params['image'] = img\n",
    "    params['show'] = 'true'\n",
    "    params = urllib.parse.urlencode(params).encode(\"utf-8\")\n",
    "    #params = json.dumps(params).encode('utf-8')\n",
    "    \n",
    "    access_token = get_token()\n",
    "    begin = time.perf_counter()\n",
    "    request_url = request_url + \"?access_token=\" + access_token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    end = time.perf_counter()\n",
    " \n",
    "    print('处理时长:'+'%.2f'%(end-begin)+'秒')\n",
    "    if content:\n",
    "        #print(content)\n",
    "        content=content.decode('utf-8')\n",
    "        #print(content)\n",
    "        data = json.loads(content)\n",
    "        words_result=data['result']\n",
    "        print ('红酒名称:',words_result['wineNameCn'])\n",
    "        if words_result['hasdetail']!=0:\n",
    "            print ('国家:',words_result['countryCn'])\n",
    "            print ('产区:',words_result['regionCn'])\n",
    "            print ('子产区:',words_result['subRegionCn'])\n",
    "            print ('酒庄:',words_result['wineryCn'])\n",
    "            print ('酒类型:',words_result['classifyByColor'])\n",
    "            print ('糖分类型:',words_result['classifyBySugar'])\n",
    "            print ('色泽:',words_result['color'])\n",
    "            print ('葡萄品种:',words_result['grapeCn'])\n",
    "            print ('品尝温度:',words_result['tasteTemperature'])\n",
    "            print ('特点说明:',words_result['description'])\n",
    "        \n",
    "redwine('7')  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
